package com.fr.chart.shape3d;

import com.fr.base.background.Background;
import com.fr.base.background.ColorBackground;
import com.fr.base.core.GraphHelper;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.BaseXMLUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.chart.base.equals.Equals;
import com.fr.chart.core.GeneralGlyph;
import com.fr.chart.math.Object3D;
import com.fr.chart.math.Projection;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Cubic extends GeneralGlyph
  implements Object3D
{
  private static final long serialVersionUID = 7339367968061606971L;
  public static final String XML_TAG = "Cubic";
  public static final byte TOP = 0;
  public static final byte BOTTOM = 1;
  public static final byte LEFT = 2;
  public static final byte RIGHT = 3;
  public static final byte FRONT = 4;
  public static final byte BACK = 5;
  private float x;
  private float y;
  private float z;
  private float width;
  private float height;
  private float deep;
  private byte[] covers;
  private Projection projection;
  private Point2D[] points;

  public Cubic()
  {
    this.x = 0.0F;
    this.y = 0.0F;
    this.z = 0.0F;
    this.deep = 60.0F;
    this.covers = new byte[] { 0, 3, 4 };
    this.projection = new Projection();
    this.points = new Point2D[8];
  }

  public Cubic(double paramDouble1, double paramDouble2, double paramDouble3, double paramDouble4)
  {
    this.x = 0.0F;
    this.y = 0.0F;
    this.z = 0.0F;
    this.deep = 60.0F;
    this.covers = new byte[] { 0, 3, 4 };
    this.projection = new Projection();
    this.points = new Point2D[8];
    this.x = (float)paramDouble1;
    this.y = (float)paramDouble2;
    this.width = (float)paramDouble3;
    this.height = (float)paramDouble4;
  }

  public Cubic(float paramFloat1, float paramFloat2, float paramFloat3, float paramFloat4, float paramFloat5, float paramFloat6)
  {
    this(paramFloat1, paramFloat2, paramFloat4, paramFloat5);
    this.z = paramFloat3;
    this.deep = paramFloat6;
  }

  public Cubic(double paramDouble1, double paramDouble2, double paramDouble3, double paramDouble4, double paramDouble5, double paramDouble6)
  {
    this.x = 0.0F;
    this.y = 0.0F;
    this.z = 0.0F;
    this.deep = 60.0F;
    this.covers = new byte[] { 0, 3, 4 };
    this.projection = new Projection();
    this.points = new Point2D[8];
    this.x = (float)paramDouble1;
    this.y = (float)paramDouble2;
    this.z = (float)paramDouble3;
    this.width = (float)paramDouble4;
    this.height = (float)paramDouble5;
    this.deep = (float)paramDouble6;
  }

  public void setCovers(byte[] paramArrayOfByte)
  {
    this.covers = paramArrayOfByte;
  }

  public void setCubic(double paramDouble1, double paramDouble2, double paramDouble3, double paramDouble4, double paramDouble5, double paramDouble6)
  {
    this.x = (float)paramDouble1;
    this.y = (float)paramDouble2;
    this.z = (float)paramDouble3;
    this.width = (float)paramDouble4;
    this.height = (float)paramDouble5;
    this.deep = (float)paramDouble6;
  }

  public double getDeep()
  {
    return this.deep;
  }

  public void setDeep(double paramDouble)
  {
    this.deep = (float)paramDouble;
  }

  public Projection getProjection()
  {
    return this.projection;
  }

  public void setProjection(Projection paramProjection)
  {
    this.projection = paramProjection;
  }

  protected Background getBrighterBackground()
  {
    Object localObject = getBackground();
    if ((localObject instanceof ColorBackground) && (((ColorBackground)localObject).getColor() != null))
      localObject = ColorBackground.getInstance(((ColorBackground)localObject).getColor().brighter());
    return ((Background)localObject);
  }

  protected Background getDarkerBackground()
  {
    Object localObject = getBackground();
    if ((localObject instanceof ColorBackground) && (((ColorBackground)localObject).getColor() != null))
      localObject = ColorBackground.getInstance(((ColorBackground)localObject).getColor().darker());
    return ((Background)localObject);
  }

  public void draw(Graphics paramGraphics)
  {
    Background localBackground1;
    Background localBackground2;
    int j;
    int k;
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    Paint localPaint = localGraphics2D.getPaint();
    Composite localComposite = localGraphics2D.getComposite();
    localGraphics2D.setComposite(AlphaComposite.getInstance(3, getAlpha()));
    init();
    if (getBackground() != null)
    {
      localBackground1 = getBrighterBackground();
      localBackground2 = getDarkerBackground();
      if (this.covers != null)
        for (k = 0; k < this.covers.length; ++k)
          switch (this.covers[k])
          {
          case 0:
            localBackground1.paint(localGraphics2D, top());
            break;
          case 1:
            localBackground1.paint(localGraphics2D, bottom());
            break;
          case 2:
            localBackground2.paint(localGraphics2D, left());
            break;
          case 3:
            localBackground2.paint(localGraphics2D, right());
            break;
          case 4:
            getBackground().paint(localGraphics2D, front());
            break;
          case 5:
            getBackground().paint(localGraphics2D, back());
          }
    }
    int i = getBorderStyle();
    if (i != 0)
    {
      localGraphics2D.setPaint(getBorderColor());
      if (this.covers != null)
        for (j = 0; j < this.covers.length; ++j)
          switch (this.covers[j])
          {
          case 0:
            GraphHelper.draw(localGraphics2D, top(), i);
            break;
          case 1:
            GraphHelper.draw(localGraphics2D, bottom(), i);
            break;
          case 2:
            GraphHelper.draw(localGraphics2D, left(), i);
            break;
          case 3:
            GraphHelper.draw(localGraphics2D, right(), i);
            break;
          case 4:
            GraphHelper.draw(localGraphics2D, front(), i);
            break;
          case 5:
            GraphHelper.draw(localGraphics2D, back(), i);
          }
    }
    localGraphics2D.setPaint(localPaint);
    localGraphics2D.setComposite(localComposite);
  }

  private void init()
  {
    this.points[0] = this.projection.projectee(this.x, this.y, this.z);
    this.points[1] = this.projection.projectee(this.x + this.width, this.y, this.z);
    this.points[2] = this.projection.projectee(this.x, this.y + this.height, this.z);
    this.points[3] = this.projection.projectee(this.x + this.width, this.y + this.height, this.z);
    this.points[4] = this.projection.projectee(this.x, this.y, this.z - this.deep);
    this.points[5] = this.projection.projectee(this.x + this.width, this.y, this.z - this.deep);
    this.points[6] = this.projection.projectee(this.x, this.y + this.height, this.z - this.deep);
    this.points[7] = this.projection.projectee(this.x + this.width, this.y + this.height, this.z - this.deep);
  }

  private GeneralPath top()
  {
    GeneralPath localGeneralPath = new GeneralPath();
    localGeneralPath.moveTo((float)this.points[0].getX(), (float)this.points[0].getY());
    localGeneralPath.lineTo((float)this.points[1].getX(), (float)this.points[1].getY());
    localGeneralPath.lineTo((float)this.points[5].getX(), (float)this.points[5].getY());
    localGeneralPath.lineTo((float)this.points[4].getX(), (float)this.points[4].getY());
    localGeneralPath.closePath();
    return localGeneralPath;
  }

  private GeneralPath bottom()
  {
    GeneralPath localGeneralPath = new GeneralPath();
    localGeneralPath.moveTo((float)this.points[2].getX(), (float)this.points[2].getY());
    localGeneralPath.lineTo((float)this.points[3].getX(), (float)this.points[3].getY());
    localGeneralPath.lineTo((float)this.points[7].getX(), (float)this.points[7].getY());
    localGeneralPath.lineTo((float)this.points[6].getX(), (float)this.points[6].getY());
    localGeneralPath.closePath();
    return localGeneralPath;
  }

  private GeneralPath right()
  {
    GeneralPath localGeneralPath = new GeneralPath();
    localGeneralPath.moveTo((float)this.points[1].getX(), (float)this.points[1].getY());
    localGeneralPath.lineTo((float)this.points[3].getX(), (float)this.points[3].getY());
    localGeneralPath.lineTo((float)this.points[7].getX(), (float)this.points[7].getY());
    localGeneralPath.lineTo((float)this.points[5].getX(), (float)this.points[5].getY());
    localGeneralPath.closePath();
    return localGeneralPath;
  }

  private GeneralPath left()
  {
    GeneralPath localGeneralPath = new GeneralPath();
    localGeneralPath.moveTo((float)this.points[0].getX(), (float)this.points[0].getY());
    localGeneralPath.lineTo((float)this.points[2].getX(), (float)this.points[2].getY());
    localGeneralPath.lineTo((float)this.points[6].getX(), (float)this.points[6].getY());
    localGeneralPath.lineTo((float)this.points[4].getX(), (float)this.points[4].getY());
    localGeneralPath.closePath();
    return localGeneralPath;
  }

  private GeneralPath front()
  {
    GeneralPath localGeneralPath = new GeneralPath();
    localGeneralPath.moveTo((float)this.points[0].getX(), (float)this.points[0].getY());
    localGeneralPath.lineTo((float)this.points[1].getX(), (float)this.points[1].getY());
    localGeneralPath.lineTo((float)this.points[3].getX(), (float)this.points[3].getY());
    localGeneralPath.lineTo((float)this.points[2].getX(), (float)this.points[2].getY());
    localGeneralPath.closePath();
    return localGeneralPath;
  }

  private GeneralPath back()
  {
    GeneralPath localGeneralPath = new GeneralPath();
    localGeneralPath.moveTo((float)this.points[4].getX(), (float)this.points[4].getY());
    localGeneralPath.lineTo((float)this.points[5].getX(), (float)this.points[5].getY());
    localGeneralPath.lineTo((float)this.points[7].getX(), (float)this.points[7].getY());
    localGeneralPath.lineTo((float)this.points[6].getX(), (float)this.points[6].getY());
    localGeneralPath.closePath();
    return localGeneralPath;
  }

  public void drawPoints(Graphics paramGraphics)
  {
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    Paint localPaint = localGraphics2D.getPaint();
    localGraphics2D.setPaint(Color.BLACK);
    localGraphics2D.drawString("(" + this.x + "," + this.y + "," + 0 + ")", (int)this.points[0].getX(), (int)this.points[0].getY());
    localGraphics2D.drawString("(" + (this.x + this.width) + "," + this.y + "," + 0 + ")", (int)this.points[1].getX(), (int)this.points[1].getY());
    localGraphics2D.drawString("(" + this.x + "," + (this.y + this.height) + "," + 0 + ")", (int)this.points[2].getX(), (int)this.points[2].getY());
    localGraphics2D.drawString("(" + (this.x + this.width) + "," + (this.y + this.height) + "," + 0 + ")", (int)this.points[3].getX(), (int)this.points[3].getY());
    localGraphics2D.drawString("(" + this.x + "," + this.y + "," + (-this.deep) + ")", (int)this.points[4].getX(), (int)this.points[4].getY());
    localGraphics2D.drawString("(" + (this.x + this.width) + "," + this.y + "," + (-this.deep) + ")", (int)this.points[5].getX(), (int)this.points[5].getY());
    localGraphics2D.drawString("(" + this.x + "," + (this.y + this.height) + "," + (-this.deep) + ")", (int)this.points[6].getX(), (int)this.points[6].getY());
    localGraphics2D.drawString("(" + (this.x + this.width) + "," + (this.y + this.height) + "," + (-this.deep) + ")", (int)this.points[7].getX(), (int)this.points[7].getY());
    localGraphics2D.setPaint(localPaint);
  }

  public Shape getShape()
  {
    int i;
    init();
    GeneralPath localGeneralPath = new GeneralPath();
    if (this.covers != null)
      for (i = 0; i < this.covers.length; ++i)
        switch (this.covers[i])
        {
        case 0:
          localGeneralPath.append(top(), false);
          break;
        case 1:
          localGeneralPath.append(bottom(), false);
          break;
        case 2:
          localGeneralPath.append(left(), false);
          break;
        case 3:
          localGeneralPath.append(right(), false);
          break;
        case 4:
          localGeneralPath.append(front(), false);
          break;
        case 5:
          localGeneralPath.append(back(), false);
        }
    return localGeneralPath;
  }

  public Shape getOutline4Fill()
  {
    return getShape();
  }

  public Iterator selectableChildren()
  {
    return Collections.EMPTY_LIST.iterator();
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str1 = paramXMLableReader.getTagName();
      if (str1.equals("Attr"))
      {
        String str2;
        if ((str2 = paramXMLableReader.getAttr("x")) != null)
          this.x = Float.valueOf(str2).floatValue();
        if ((str2 = paramXMLableReader.getAttr("y")) != null)
          this.y = Float.valueOf(str2).floatValue();
        if ((str2 = paramXMLableReader.getAttr("z")) != null)
          this.z = Float.valueOf(str2).floatValue();
        if ((str2 = paramXMLableReader.getAttr("width")) != null)
          this.width = Float.valueOf(str2).floatValue();
        if ((str2 = paramXMLableReader.getAttr("height")) != null)
          this.height = Float.valueOf(str2).floatValue();
        if ((str2 = paramXMLableReader.getAttr("deep")) != null)
          this.deep = Float.valueOf(str2).floatValue();
      }
      else if (str1.equals("Projection"))
      {
        this.projection = ((Projection)paramXMLableReader.readXMLObject(new Projection()));
      }
      else if (str1.equals("Covers"))
      {
        this.covers = BaseXMLUtils.readByteArray(paramXMLableReader);
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("Cubic").attr("class", getClass().getName());
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.startTAG("Attr").attr("x", this.x).attr("y", this.y).attr("z", this.z).attr("width", this.width).attr("height", this.height).attr("deep", this.deep).end();
    if ((this.covers != null) && (this.covers.length > 0))
    {
      paramXMLPrintWriter.startTAG("Covers");
      BaseXMLUtils.writeByteArray(paramXMLPrintWriter, this.covers);
      paramXMLPrintWriter.end();
    }
    if (this.projection != null)
      this.projection.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof Cubic))
      return false;
    Cubic localCubic = (Cubic)paramObject;
    if (!(super.equals(localCubic)))
      return false;
    if (localCubic.getDeep() != getDeep())
      return false;
    return (Equals.equals(localCubic.getProjection(), getProjection()));
  }

  public JSONObject toJSONObject()
    throws JSONException
  {
    JSONObject localJSONObject = super.toJSONObject();
    localJSONObject.put("x", this.x);
    localJSONObject.put("y", this.y);
    localJSONObject.put("z", this.z);
    localJSONObject.put("width", this.width);
    localJSONObject.put("height", this.height);
    localJSONObject.put("deep", this.deep);
    localJSONObject.put("points", this.points);
    localJSONObject.put("projection", this.projection.toJSONObject());
    return localJSONObject;
  }
}